﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine.Draw;
using PloobsEngine.SceneControl;

namespace Etapa1.SteeringExperiments
{
    public class Flee : Steering
    {
        public Flee(Vector3 target, float panicDistance)
            : base(BehaviorType.flee)
        {
            this.panicDistance = panicDistance;
            this.target = target;
            isObj = false;
        }

        public Flee(IObject target, float panicDistance)
            : base(BehaviorType.flee)
        {
            this.panicDistance = panicDistance;
            this.objTarget = target;
            isObj = true;
        }

        private Vector3 target;
        private IObject objTarget;
        bool isObj;
        private float panicDistance;

        public float PanicDistance
        {
            get { return panicDistance; }
            set { panicDistance = value; }
        }

        public Vector3 Target
        {
            get { return target; }
            set { target = value; isObj = false; }
        }        

        public IObject ObjTarget
        {
            get { return objTarget; }
            set { objTarget = value; isObj = true; }
        }

        public override Microsoft.Xna.Framework.Vector3 CalculateSteering(SteeringEntityInfo entity)
        {

            Drawing.DrawCircleOrDisk(PanicDistance, Vector3.Up, entity.Obj.Position, Color.Purple, 100, false, true);

            Vector3 DesiredVelocity = Vector3.Zero;
            if (isObj == true)
            {
                if (Vector3.Distance(entity.Obj.Position, objTarget.Position) <= this.PanicDistance)
                {
                    DesiredVelocity = Vector3.Normalize(entity.Obj.Position - objTarget.Position) * entity.MaxVelocity;
                }
            }
            else
            {
                if (Vector3.Distance(entity.Obj.Position, target) <= this.PanicDistance)
                {
                    DesiredVelocity = Vector3.Normalize(entity.Obj.Position - target) * entity.MaxVelocity;
                }
            }

            return (DesiredVelocity - entity.Obj.PhysicObject.Velocity);
        }
    }
}
